<?php
namespace App\Http\Controllers\Login;
use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
class ConfigAjaxController extends ApiBaseController {
  public function __construct(Request $request) {
    parent::__construct($request);
  }
	/**
	* 基础配置修改提交操作
	*/
	public function createSave(){
		$partner_discount=\App\Components\Tools::checkDiscountTwo($this->params['partner_discount']);
		!$partner_discount ? $this->returnJson(1,"请正确填写合伙人充值比例（小于等于100的数值，最多一位小数）") : '';
		$vip_discount=\App\Components\Tools::checkDiscount($this->params['vip_discount']);
		!$vip_discount ? $this->returnJson(1,"请正确填写用户充值折扣（小于10的数值，最多一位小数）") : '';
		if(!preg_match("/^[1-9]{1}[0-9]{0,}$/",$this->params['partner_amount'])){
			$this->returnJson(1,"请正确填写合伙人最低充值金额");
		}
		if(!preg_match("/^[1-9]{1}[0-9]{0,}$/",$this->params['vip_amount'])){
			$this->returnJson(1,"请正确填写用户最低充值金额");
		}
		$partner_ratio=\App\Components\Tools::checkDiscount($this->params['partner_ratio']);
		!$partner_ratio ? $this->returnJson(1,"请正确填写合伙人分佣比例（小于10的数值，最多一位小数）") : '';
		
		$checkUrl=\App\Components\Tools::checkUrl($this->params['service_url']);
		!$checkUrl ? $this->returnJson(1,"请正确填写在线客服url") : '';
		
		$path='/config/config.json';
		$data=\App\Components\Tools::getThird($path);
		if(!is_array($data)){
			$data=array();
		}
		if(isset($this->params['typeName']) && $this->params['typeName']=='basics'){
			$arr=array(
				'basics',
				array(
					'partner_discount','vip_discount','partner_amount','wechat','vip_amount',
					'partner_ratio','service_code','service_mobile','service_url','service_qq','certification'
				)
			);
			$this->configArr($data,$arr,$path);
			$this->returnJson(0,"保存成功");
		}
		$this->returnJson(1,"参数错误");
	}


	/**
	*  第三方配置保存修改操作
	*  typeName  各大接口配置名称 	data (配置数据)		arr (需要配置的数据字段名) 	configArr (配置数据封装)
	*  qiniu (七牛云)    aliyun (阿里云)   zhifb (支付宝)	weixin (微信)	douyin (抖音)	kuaish (快手)	weishi (微视)
	*/
	public function thirdSave(){
		$path='/config/third.json';
		$data=\App\Components\Tools::getThird($path);
		if(!is_array($data)){
			$data=array();
		}
		
		if(isset($this->params['typeName']) && $this->params['typeName']=='qiniu'){
			$arr=array('qiniu',array('qnAccesskey','qnSecretkey','qnBucket','qnUrl','qnBankUrl'));
			$this->configArr($data,$arr,$path);
			
		}elseif(isset($this->params['typeName']) && $this->params['typeName']=='aliyun'){
			$arr=array('aliyun',array('alAccesskey','alAccessKeySecret','alBucket','alUrl','alBankUrl'));
			$this->configArr($data,$arr,$path);
			
		}elseif(isset($this->params['typeName']) && $this->params['typeName']=='zhifb'){
			$arr=array('zhifb',array('zfbCltName','zfbClbt','zfbSecretKey','zfbUrl','zfbBankUrl'));
			$this->configArr($data,$arr,$path);
			
		}elseif(isset($this->params['typeName']) && $this->params['typeName']=='weixin'){
			$arr=array('weixin',array('wxAccessKeyId','wxAccessKeySecret','wxBucket','wxUrl','wxBankUrl'));
			$this->configArr($data,$arr,$path);
			
		}elseif(isset($this->params['typeName']) && $this->params['typeName']=='douyin'){
			$arr=array('douyin',array('dy_client_key','dy_client_secret','dy_url','dy_bank_url'));
			$this->configArr($data,$arr,$path);
			
		}elseif(isset($this->params['typeName']) && $this->params['typeName']=='kuaish'){
			$arr=array('kuaish',array('ks_client_key','ks_client_secret','ks_url','ks_bank_url'));
			$this->configArr($data,$arr,$path);
			
		}elseif(isset($this->params['typeName']) && $this->params['typeName']=='weishi'){
			$arr=array('weishi',array('ws_client_key','ws_client_secret','ws_url','ws_bank_url'));
			$this->configArr($data,$arr,$path);
		}else{
			$this->returnJson(1,"参数错误");
		}
		$this->returnJson(0,"保存成功");
	}
	
	/**
	* 套餐配置保存修改操作
	*/
	public function inquirySave(){
		if( isset($this->params['iqprice']) && !preg_match("/^[1-9]{1}[0-9]{0,}$/",$this->params['iqprice']) ){
			$this->returnJson(1,"请正确输入套餐价格");
		}
		if( isset($this->params['iqkeymber']) && !preg_match("/^[1-9]{1}[0-9]{0,}$/",$this->params['iqkeymber']) ){
			$this->returnJson(1,"请正确输入视频上传数");
		}
		if( isset($this->params['iqnumber']) && !preg_match("/^[1-9]{1}[0-9]{0,}$/",$this->params['iqnumber']) ){
			$this->returnJson(1,"请正确输入询盘数");
		}
		if( isset($this->params['iqmonumber']) && !preg_match("/^[1-9]{1}[0-9]{0,}$/",$this->params['iqmonumber']) ){
			$this->returnJson(1,"请正确输入作品监控数量");
		}
		if( isset($this->params['iqtopnumber']) && !preg_match("/^[1-9]{1}[0-9]{0,}$/",$this->params['iqtopnumber']) ){
			$this->returnJson(1,"请正确输入主页监控数量");
		}
		if( isset($this->params['xun_number']) && !preg_match("/^[1-9]{1}[0-9]{0,}$/",$this->params['xun_number']) ){
			$this->returnJson(1,"请正确输入每日询盘上限数量");
		}
		if( isset($this->params['info_number']) && !preg_match("/^[1-9]{1}[0-9]{0,}$/",$this->params['info_number']) ){
			$this->returnJson(1,"请正确输入账户监控数");
		}
		
		$path='/config/inquiry.json';
		$data=\App\Components\Tools::getThird($path);
		if(!is_array($data)){
			$data=array();
		}
		if(isset($this->params['typeName'])){
			$arr=array($this->params['typeName'],array('iqname','iqprice','iqkeymber','iqnumber','iqmonumber','iqtopnumber','xun_number','info_number'));
			$this->configArr($data,$arr,$path);
			$this->returnJson(0,"保存成功");
		}
		$this->returnJson(1,"参数错误");
	}
	
	//配置数据封装
	public function configArr($data,$arr,$path){
		foreach($arr[1] as $k=>$v){
			if(!isset($this->params[$v])){
				$this->returnJson(1,"参数错误");
			}
			$data[$arr[0]][$v]=$this->params[$v];
		}
		\App\Components\Tools::setThird($path,json_encode($data));
	}
}
